package cn.chrisx.drad.admob;

import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;

import com.google.android.gms.ads.AdView;

/**
 * @author Jzfox <jz_boy@163.com>
 * @version 1.0
 * @date 2018/9/26 11:29
 * @description
 * @copyright ©2018 www.jzfox.net All rights reserved.
 */
public class BannerImpl extends BaseAd {

    private AdView adView;

    @Override
    public boolean loadAd(Context context) {
        super.loadAd(context);

        if (adView == null) {
            adView = new AdView(context);
            adView.setAdUnitId(drAdConfig.getAdUnitId());

            adView.setAdListener(googleAdListener);

            if (drAdConfig instanceof AdMobAdConfig) {
                adView.setAdSize(((AdMobAdConfig) drAdConfig).getAdSize());
            }
        }

        adView.loadAd(adRequest);
        return true;

    }

    @Override
    public boolean show() {

        if (isValid() && drAdConfig instanceof AdMobAdConfig) {

            ViewGroup root = ((AdMobAdConfig) drAdConfig).getAdContainer();
            if (root == null) return false;

            root.addView(adView);
            return true;

        }

        return false;
    }


    @Override
    public void onPause(Activity activity) {
        super.onPause(activity);
        if (adView != null) adView.pause();
    }

    @Override
    public void onResume(Activity activity) {
        super.onResume(activity);
        if (adView != null) adView.resume();
    }

    @Override
    public boolean isValid() {
        return adView != null;
    }

    @Override
    public void onDestroy(Context context) {
        super.onDestroy(context);
        if (adView != null) adView.destroy();
    }
}
